package com.netease.charge.controller.util;

import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.ToolManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.StringWriter;

/**
 * @author hzhuqun
 * @create 2016-09-29 上午11:30
 **/

@Component
public class VelocityUtil {

    @Resource
    private VelocityEngine velocityEngine;

    public String load(){
        // 加载模板，设定模板编码
        Template t = velocityEngine.getTemplate("test1.vm", "UTF-8");
        // 加载toolbox
        ToolManager manager = new ToolManager();
        manager.configure("toolbox.xml");
        Context velocityContext = manager.createContext();
        velocityContext.put("bill",112);
        // 设置输出
        StringWriter writer = new StringWriter();
        // 将环境数据转化输出
        t.merge(velocityContext, writer);
        return writer.toString();
    }
}
